home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-02  |  4.8 KB  |  164 lines

  1. /* 
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version. 
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU General Public License
  20.   along with this program; if not, write to the Free Software
  21.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  22.  
  23. */
  24.  
  25. /*******************************************************************/
  26. /*                                                                 */
  27. /* Disk usage module, Demosthenes Skipitaris                       */
  28. /*                                                                 */
  29. /*******************************************************************/
  30.  
  31. #include "cf.defs.h"
  32. #include "cf.extern.h"
  33. #include "../pub/getopt.h"
  34.  
  35. #ifdef HAVE_SYS_STATFS_H
  36. #include <sys/statfs.h>
  37. #endif
  38.  
  39. #ifdef HAVE_SYS_VFS_H
  40. #include <sys/vfs.h>
  41. #endif
  42.  
  43. #ifdef HAVE_SYS_STATVFS_H
  44. #include <sys/statvfs.h>
  45. #endif
  46.  
  47. /************************************************************************/
  48.  
  49. GetDiskUsage (file,type)
  50.  
  51. char *file;
  52. enum cfsizes type;
  53.  
  54. {
  55. #if defined SOLARIS || OSF
  56.     struct statvfs buf;
  57. #elif defined ULTRIX
  58.     struct fs_data buf;
  59. #else
  60.     struct statfs buf;
  61. #endif
  62.     u_long blocksize = 1024, total = 0, used = 0, avail = 0;
  63.     int capacity = 0;
  64.  
  65.     bzero (&buf, sizeof (buf));
  66.  
  67.     Verbose("Checking free space on %s\n",file);
  68.  
  69.  
  70. #if defined ULTRIX
  71.     if (getmnt (NULL, &buf, sizeof (struct fs_data), STAT_ONE, file) == -1)
  72.        {
  73.        sprintf(OUTPUT,"Couldn't get filesystem info for %s\n",file);
  74.        CfLog(cferror,OUTPUT,"");
  75.        return cfinfinity;
  76.        }
  77. #elif defined SOLARIS || defined OSF
  78.     if (statvfs (file, &buf) != 0)
  79.        {
  80.        sprintf(OUTPUT,"Couldn't get filesystem info for %s\n",file);
  81.        CfLog(cferror,OUTPUT,"");
  82.        return cfinfinity;
  83.        }
  84. #elif defined IRIX || defined SCO || defined CFCRAY || defined UNIXWARE
  85.     if (statfs (file, &buf, sizeof (struct statfs), 0) != 0)
  86.        {
  87.        sprintf(OUTPUT,"Couldn't get filesystem info for %s\n",file);
  88.        CfLog(cferror,OUTPUT,"");
  89.        return cfinfinity;
  90.        }
  91. #else
  92.     if (statfs (file, &buf) != 0)
  93.        {
  94.        sprintf(OUTPUT,"Couldn't get filesystem info for %s\n",file);
  95.        CfLog(cferror,OUTPUT,"");
  96.        return cfinfinity;
  97.        }
  98. #endif
  99.  
  100. #if defined ULTRIX
  101.     total = buf.fd_btot;
  102.     used = buf.fd_btot - buf.fd_bfree;
  103.     avail = buf.fd_bfreen;
  104. #endif
  105.  
  106. #if defined SOLARIS
  107.     total = buf.f_blocks * (buf.f_frsize / blocksize);
  108.     used = (buf.f_blocks - buf.f_bfree) * (buf.f_frsize / blocksize);
  109.     avail = buf.f_bavail * (buf.f_frsize / blocksize);
  110. #endif
  111.  
  112. #if defined NETBSD || defined FREEBSD || defined OPENBSD || defined LINUX || defined SUNOS || defined HPuUX
  113.     total = buf.f_blocks;
  114.     used = buf.f_blocks - buf.f_bfree;
  115.     avail = buf.f_bavail;
  116. #endif
  117.  
  118. #if defined OSF
  119.     total = (buf.f_blocks *  buf.f_frsize) / blocksize;
  120.     used = ((buf.f_blocks - buf.f_bfree)* buf.f_frsize) / blocksize;
  121.     avail = (buf.f_bavail * buf.f_frsize) / blocksize;
  122. #endif
  123.  
  124. #if defined AIX
  125.     total = buf.f_blocks * (buf.f_bsize / blocksize);
  126.     used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
  127.     avail = buf.f_bfree * (buf.f_bsize / blocksize);
  128. #endif
  129.  
  130. #if defined SCO
  131.       total = buf.f_blocks * (buf.f_bsize / blocksize);
  132.       used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
  133.       avail = buf.f_bfree * (buf.f_bsize / blocksize);
  134. #endif
  135.     
  136. #if defined CFCRAY
  137.     total = buf.f_blocks * (buf.f_bsize / blocksize);
  138.     used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
  139.     avail = buf.f_bfree * (buf.f_bsize / blocksize);
  140. #endif
  141.  
  142. #if defined IRIX
  143.     /* Float fix by arjen@sara.nl */
  144.     total = buf.f_blocks *  ((float)buf.f_bsize / blocksize);
  145.     used = (buf.f_blocks - buf.f_bfree) * ((float)buf.f_bsize / blocksize);
  146.     avail = buf.f_bfree *  ((float)buf.f_bsize/blocksize);
  147. #endif
  148.    
  149. capacity = (double) (avail) / (double) (avail + used) * 100;
  150.  
  151. Debug2("GetDiskUsage(%s) = %d/%d\n",file,avail,capacity);
  152.     
  153. /* Free kilobytes */
  154.  
  155. if (type == cfabs)
  156.    {
  157.    return avail;
  158.    }
  159. else
  160.    {
  161.    return capacity;
  162.    }
  163. }
  164.